#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
sys.path.insert(0, os.path.split(__file__)[0])
from toughportal.common import choosereactor
choosereactor.install_optimal_reactor(True)
from twisted.internet import reactor
from toughportal.console import portal_app
from toughportal.server import portal_server
from toughportal.common import config as iconfig
import argparse


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-listen', '--listen', action='store_true', default=False, dest='listen', help='listen option')
    parser.add_argument('-portal', '--portal', action='store_true', default=False, dest='portal', help='portal option')
    parser.add_argument('-port', '--port', type=int, default=0, dest='port', help='portal/listen port')
    parser.add_argument('-c', '--conf', type=str, default="/etc/toughportal.conf", dest='conf', help='config file')
    parser.add_argument('-debug', '--debug', action='store_true', default=False, dest='debug', help='debug option')
    parser.add_argument('-x', '--xdebug', action='store_true', default=False, dest='xdebug', help='xdebug option')
    args = parser.parse_args(sys.argv[1:])
    config = iconfig.find_config(args.conf)

    if args.debug or args.xdebug:
        config.defaults.debug = True

    if args.listen:
        if args.port:
            config.portal.listen = args.port
        portal_server.run(config)
        reactor.run()

    if args.portal:
        if args.port:
            config.portal.port = args.port
        portal_app.run(config)
        reactor.run()




